<?php

require_once "./PostView.php";
require_once "./PostHandler.php";
require_once "./LoginHandler.php";
require_once "./Database.php";
require_once "./Validation.php";

class PostController {
	
	private $statusMessage = "";
	
	//Kallar på instanser för Handler/View
	public function PostDoControll(Database $db) {
			$ph = new PostHandler($db);
			$pv = new PostView();
			$val = new Validator();
			$lh = new LoginHandler($db);
			
			$html = "";
				//TriedToPost och TriedToDeletePost kollar helt enkelt bara om knapparna tryckts på via handlerns isset-funktioner.
				if($pv -> TriedToPost()){
					if(!$val->validatePost($pv->GetTopic()) || !$val->validatePost($pv->GetText()))
					{
					 	$this->statusMessage .= $pv->statusMessage(PostView::POSTVALFAIL);
					}
					else{
					 	$this->statusMessage .= $pv->statusMessage(PostView::POSTSUCCESS);
						$ph -> DoPost($pv->GetTopic(), $pv->GetText());
					}	
				}	
				if($pv -> TriedToDeletePost()){
							
					//Tar bort via ID
					$ph -> DoDeletePost($pv->GetMessageIDDelete());
				}
	
	//Trycker in Postform och viewposts som visar alla poster
	if($lh->IsLoggedIn()){
		$html .= $pv -> DoPostBox();
		$html .= $pv -> ViewPosts($ph->DoCheckPosts($id, $topic, $text));
	}
	else {
		$this->statusMessage .= $pv->statusMessage(PostView::NOTLOGGEDIN);
	}
		return $html. $this->statusMessage;	
	}

}

?>
